#include <stdio.h>
#include <ogcsys.h>
#include <unistd.h>
#include <stdlib.h>

#include "getfile.h"
#include "network.h"
#include "video.h"
#include "imagedraw.h"
#include "wpad.h"

enum {
	downloadname
};

struct download {
	char downloadname[ISFS_MAXPATH];
	char homebrewname[ISFS_MAXPATH];
};

/* List of downloads */
static struct download download_list[] = {
	{"homebrew_browser_v0.2.5c.zip", "Homebrewbrowser 0.2.5c"},     //hier können mehr hinzugefügt werden
	{"DimokRemakeV3.zip","Dimok Remake V3"}, //und ein Ersatzname gewählt werden
    {"flussdiagramm.pdf","Unser Flussdiagramm als PDF"}
} ;

/* Macros */
#define NB_DOWNLOADS	(sizeof(download_list) / sizeof(struct download))

/* Constants */
#define CONSOLE_XCOORD		20
#define CONSOLE_YCOORD		230

#define ENTRIES_PER_PAGE	8

/* Variables */
static s32 selected = 0, start = 0;


void restart_wait(void)
{
	printf("\n    Press any button to return to Homebrew Channel...");
	fflush(stdout);

	/* Wait for any button */
	wpad_waitbuttons();

	printf("\n Returning...\n\n");

	/* Load system menu */
	exit(0);
}

void print_disclaimer(void)
{
	/* Print disclaimer */
	printf("Hallo:\n\n");

	printf("    Dies ist ein test fuer den WiFi download.\n");
}

void print_list(void)
{
	u32 cnt, idx;

	printf("Beispiel downloads:\n\n");

	for (cnt = 0, idx = start; cnt < ENTRIES_PER_PAGE && idx < NB_DOWNLOADS; cnt++, idx++) {
		struct download *fw = & download_list[idx];

		/* Selected entry */
		(idx == selected) ? printf("\t>> ") : printf("\t   ");
		fflush(stdout);

		/* Print download info */
		printf("Download: %s\n",
			fw->homebrewname);
	}

}

void scroll_list(void)
{
	s32 index;

	/* Out of list limits? */
	if (selected < 0)
		selected = NB_DOWNLOADS - 1;

	if (selected >= NB_DOWNLOADS)
		selected = 0;

	/* List scrolling */
	index = (selected - start);

	if (index < 0)
		start += index;

	if (index >= ENTRIES_PER_PAGE)
		start += (index - ENTRIES_PER_PAGE) + 1;
}

void controls(void)
{
	s32 ret = 0;
	u32 buttons = wpad_waitbuttons();

    struct download *fw = & download_list[selected];

	/* UP button */
	if (buttons & WPAD_BUTTON_UP) {
		selected--;
		scroll_list();
	}

	/* DOWN button */
	if (buttons & WPAD_BUTTON_DOWN) {
		selected++;
		scroll_list();
	}

	/* A button */
	if (buttons & WPAD_BUTTON_A) {
		printf("\nSie ziehen %s...", fw->downloadname);
        ret = Title_DownloadFile(&fw->downloadname);
	}

    if (ret < 0)
        return;

	/* HOME button */
	if (buttons & WPAD_BUTTON_HOME) {
		printf("\nReturning.....");
		exit(0);
	}
}

int wii_error()
{
	/* Restart Wii */
	restart_wait();

	return 0;
}

int main(int argc, char **argv)
{
	s32 ret;

	/* Initialize subsystems */
	VIDEO_Init();

	/* Initialize Wiimote */
	wpad_init();

	/* Set video mode */
	video_setmode();

    /*Hintergrund darstellen*/
    Background_Show();

	/* Initialize console */
	con_init(CONSOLE_XCOORD, CONSOLE_YCOORD);



	printf("Initializing network, please wait...");


	/* Initialize network */
	ret = network_init();
	if (ret < 0) {
		printf(" ERROR! (ret = %d)\n", ret);
		wii_error();
	}


	printf(" %s\n", network_getip());

	printf("\n\n");

	/* Print disclaimer */
	print_disclaimer();

    printf("\tSollte nix passieren aber trotzdem:\n\n");
	printf(">>  If you agree, press A button to continue.\n");
	printf(">>  Otherwise, press B button to return to Homebrew Channel.\n\n");

	/* Wait for user answer */
	while (1) {
		u32 buttons = wpad_waitbuttons();

		/* A button */
		if (buttons & WPAD_BUTTON_A)
			break;

		/* B button */
		if (buttons & WPAD_BUTTON_B) {
            printf("Returning to Homebrew Channel...");
			exit(0);
		}
	}


	con_clear();

	while(1) {


		/* Print download list */
		print_list();

		printf("\n\n");

		printf("[+] Press HOME button to exit.\n");
		printf("    Press UP/DOWN to move beetwen entries.\n");
		printf("    Press A button to download file.\n");


		/* Controls */


		controls();
		con_clear();

	}

	/* Restart Wii */
	restart_wait();

	return 0;
}
